//
//  PPPetListViewController.m
//  pet
//
//  Created by 陈无忌 on 11/20/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPPetListViewController.h"
#import "PPPetInfoTableViewController.h"
#import "PPAddPetTableViewController.h"
#import "PPEditPetInfoTableViewController.h"
#import "PPSystemConfiguraton.h"
#import "supportingHeadFiles.h"
#import "PPPetCollectionViewCell.h"

@interface PPPetListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *petContainerView;
@property (nonatomic, strong) NSMutableArray *petArray;
@property (nonatomic, strong) PPSystemConfiguraton *config;
@property (nonatomic, strong) PPPetInfo *selectPet;
@property (weak, nonatomic) IBOutlet UICollectionView *petCollectionView;

@property (nonatomic) int selectRow;
@property (weak, nonatomic) IBOutlet UIView *container;

@end

@implementation PPPetListViewController

- (PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

- (NSMutableArray *)petArray
{
    if (!_petArray) {
        _petArray = [NSMutableArray array];
    }
    return _petArray;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self petData];
}

- (void)petData{
    self.petArray = [self.config.petsArray mutableCopy];
    
    [self.petCollectionView reloadData];
    if (self.petArray.count > 0) {
        PPPetInfoTableViewController *petInfoVC = self.childViewControllers.firstObject;
        self.selectPet = [self.petArray objectAtIndex:self.selectRow];
        petInfoVC.pet = self.selectPet;
        [petInfoVC viewReload];
        self.container.hidden = NO;
        NSIndexPath *index = [NSIndexPath indexPathForRow:self.selectRow inSection:0];
        [self.petCollectionView selectItemAtIndexPath:index animated:YES scrollPosition:UICollectionViewScrollPositionBottom];
    }else{
        //没有宠物
        self.container.hidden = YES;
        [UIAlertView showWithTitle:@"提示" message:@"您还没有添加宠物，是否添加一只" cancelButtonTitle:@"返回" otherButtonTitles:@[@"现在就去"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1) {
                //去宠物相册页
                PPAddPetTableViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPAddPetTableViewController"];
                [self.navigationController pushViewController:targetVC animated:YES];
            }else{
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
}


- (void)petViewReload{
    [self.config getPetListwithComepletion:^{
        self.selectRow = 0;
        [self petData];
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectRow = 0;
    self.petCollectionView.delegate = self;
    self.petCollectionView.dataSource = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(petViewReload) name:@"petListRelod" object:nil];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollection View
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.petArray.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PPPetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"petCell" forIndexPath:indexPath];
    if (indexPath.row == self.petArray.count) {
        [cell.petAvatar setImage:[UIImage imageNamed:@"addPetGray"]];
        cell.petAvatar.layer.borderWidth = 1.0;
        cell.petAvatar.layer.borderColor = [UIColor clearColor].CGColor;
    }else{
        if (indexPath.row == self.selectRow) {
            cell.petAvatar.layer.borderWidth = 1.0;
            cell.petAvatar.layer.borderColor = [UIColor blackColor].CGColor;
        }else{
            cell.petAvatar.layer.borderWidth = 1.0;
            cell.petAvatar.layer.borderColor = [UIColor clearColor].CGColor;
        }
        PPPetInfo *pet = [self.petArray objectAtIndex:indexPath.row];
        [cell.petAvatar setImageWithURL:[PPImageUrl getImageUrl:pet.petAvatar] placeholderImage:[UIImage imageNamed:@"defaultAvatar"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.petArray.count) {
        PPAddPetTableViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPAddPetTableViewController"];
        [self.navigationController pushViewController:targetVC animated:YES];
    }else{
        self.selectRow = indexPath.row;
        PPPetInfo *pet = [self.petArray objectAtIndex:indexPath.row];
        self.selectPet = pet;
        PPPetInfoTableViewController *petInfoVC = self.childViewControllers.firstObject;
        petInfoVC.pet = pet;
        [petInfoVC viewReload];
        [self.petCollectionView reloadData];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Custom IBAction
- (IBAction)goEdit:(id)sender {
    PPEditPetInfoTableViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPEditPetInfoTableViewController"];
    targetVC.pet = self.selectPet;
    [self.navigationController pushViewController:targetVC animated:YES];
}

@end
